/*! @file timeconvdlg.cpp
 * @brief Implements TimeConvDlg Class
 * @date 05.09.2012
 */

#include "timeconvdlg.h"

#include <QListWidgetItem>

/** @brief TimeConvDlg Constructor
 * @callgraph
 * @callergraph
 */
TimeConvDlg::TimeConvDlg(QString ts, int max, QList<QString> header, QWidget *parent) :
    QDialog(parent)
{
    setupUi(this);
    connect(okButton, SIGNAL(clicked()), this, SLOT(on_okClicked()));
    connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    connect(ffRadioButton, SIGNAL(clicked(bool)), this, SLOT(setFF()));
    connect(andRadioButton, SIGNAL(clicked(bool)), this, SLOT(setAnd()));
    connect(chromeRadioButton, SIGNAL(clicked(bool)), this, SLOT(setChrome()));
    connect(selectColumnCheckBox, SIGNAL(toggled(bool)),
            this, SLOT(on_toggleColumnSelection(bool)));

    progressBar->setEnabled(true);
    progressBar->setMinimum(0);
    progressBar->setMaximum(max);
    progressBar->hide();

    // fill header selection widget
    for (int h=0; h<header.size(); h++) {
        colSelListWidget->addItem(header.at(h));
        colSelListWidget->item(h)->setCheckState(Qt::Unchecked);
    }

    selection = FileType::FT_FIREFOX;

    // time conversions
    tmptime = ts.toULongLong()/1000000;
    QDateTime result = QDateTime::fromTime_t(tmptime);
    ffTimeLabel->setText(result.toString("yyyy-MM-dd hh:mm:ss"));
    tmptime = ts.toULongLong()/1000;
    result = QDateTime::fromTime_t(tmptime);
    andTimeLabel->setText(result.toString("yyyy-MM-dd hh:mm:ss"));
    unixTimeLabel->setText(ts);
    tmptime = (ts.toULongLong()/1000000) - 11644473600;
    result = QDateTime::fromTime_t(tmptime);
    chromeTimeLabel->setText(result.toString("yyyy-MM-dd hh:mm:ss"));
}

//! @brief on OK clicked
void TimeConvDlg::on_okClicked() {
    QList<int> selectedHeaderIdx;
    if (selectColumnCheckBox->checkState() == Qt::Checked) {
        for (int c=0; c<colSelListWidget->count(); c++) {
            if (colSelListWidget->item(c)->checkState() == Qt::Checked) {
                selectedHeaderIdx += c;
            }
        }
    }

    Q_EMIT gotTimeConv(selection, selectedHeaderIdx);
}

//! @brief set Column selectable true or not
void TimeConvDlg::on_toggleColumnSelection(bool on) {
    if(on)
        colSelListWidget->setEnabled(true);
    else
        colSelListWidget->setEnabled(false);
}

//! @brief set filetype
void TimeConvDlg::setAnd() {
    selection = FileType::FT_ANDROID;
}

//! @brief set filetype
void TimeConvDlg::setChrome() {
    selection = FileType::FT_CHROME;
}

//! @brief set filetype
void TimeConvDlg::setFF() {
    selection = FileType::FT_FIREFOX;
}

/**
 * @brief
 *
 * @fn TimeConvDlg::setProgress
 * @param value
 */
void TimeConvDlg::setProgress(int value) {
    progressBar->setValue(value);
}
